{layout name="./header" /}

<div class="layui-body" style="bottom: 0;border-left: solid 2px #1AA094;" id="container">
    <div class="layui-tab admin-nav-card layui-tab-brief" lay-filter="admin-tab">
        <div class="demoTable">
            搜索名称：
            <div class="layui-inline">
                <input class="layui-input" name="id" id="demoReload" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="search">搜索</button>
            <button class="layui-btn" data-type="reload">刷新</button>
            <button class="layui-btn layui-btn-xs" style="position: relative;"><input type="file" style="height:40px;width:100px;left:0px;  opacity:0; position: absolute;" id="uploads">从文本导入</button>
            <button class="layui-btn" data-type="add">增加</button>
        </div>
        <div class="layui-tab-content" style="min-height: 150px; padding: 5px 0 0 0;">
            <div class="layui-tab-item layui-show">
                <table id="demo"  lay-filter="demo"></table>
            </div>
        </div>
    </div>
</div>

<script src="__STATIC__/plugins/layui/layui.js"></script>


<script type="text/html" id="checkboxTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="lock" value="{{d.id}}" title="开放" lay-filter="lockDemo" {{ d.lock? '' : 'checked' }}>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="share">分享连接</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="sort">问题排序</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="tongji">统计</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="question">添加问题</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table','form','layer'], function(){
        var table = layui.table;
        var layer=layui.layer;
        var form=layui.form;
        var $ = layui.$;
        //方法级渲染
        var height=$('.layui-body').height();
        var demo=table.render({
            elem: '#demo'
            ,url: '{:url("questionnaire/index")}'
            ,cellMinWidth: 80,
            height:height
            ,cols: [[ //表头
                {checkbox: true, fixed: true}
                ,{field: 'id', title: 'ID', sort: true}
                ,{field: 'name', title: '问卷名称',width:'250' }
                ,{field: 'data', title: '问题数量',width:'100', sort: true}
                ,{field: 'count_people', title: '回答人数',width:'300', sort: true}
                ,{field:'lock', title:'开放回答', width:120, templet: '#checkboxTpl', unresize: true}
                ,{ align:'center',width:'auto', toolbar: '#barDemo'}
            ]]
            ,id: 'testReload'
            ,page: true
        });
        table.on('tool(demo)',function(obj){
            var data = obj.data;
           if(obj.event === 'del'){
               layer.confirm('确认删除行么？', {icon: 3, title:'提示'}, function(index){
                   console.log(666);
                   var id=data.id;
                   $.post('{:url("questionnaire/del")}',{id:id},function(respance){
                       console.log(respance);
                       if(respance.code=='200'){
                           layer.msg(respance.msg);
                           layer.close(index);
                           demo.reload();
                       }
                   })
               });
            }
            else if(obj.event=='share'){
               var  data=obj.data;
               var url="{:url('answer/answer','',true,true)}";
               layer.alert(url+'?id='+data.id, {
                   skin: 'layui-layer-molv' //样式类名
                   ,closeBtn: 0
               });
           }
            else if(obj.event === 'edit'){
               layer.open({
                   type: 2,
                   title: '问卷编辑',
                   shadeClose: true,
                   shade: 0.8,
                   area: ['800px', '600px'],
                   content: '{:url("questionnaire/category_edit")}?id='+data.id,//iframe的url
                   end:function(){
                       demo.reload();
                   }
               });

           }
            else if(obj.event === 'detail'){
               layer.open({
                   type: 2,
                   maxmin: true,
                   content: '/admin/answer/answer?id='+data.id,
                   area: ['800px', '500px'],
                   btn: ['确定', '取消'],
                   offset: '100px'
               });
           }
           else if(obj.event === 'question'){
               layer.open({
                   type: 2,
                   maxmin: true,
                   content: '/admin/questionnaire/edit?id='+data.id,
                   area: ['800px', '500px'],
                   offset: '100px',
                   end:function(){
                       demo.reload();
                   }
               });
           }else if(obj.event==='tongji'){
              window.open("{:url('questionnaire/tongji',[],true)}?id="+data.id);
           }
            else if(obj.event==='sort'){
               layer.open({
                   type: 2,
                   title: '问题排序',
                   shadeClose: true,
                   shade: 0.8,
                   area: ['550px', '500px'],
                   btn: ['确定', '取消'],
                   content: '{:url("questionnaire/sort")}?id='+data.id,//iframe的url
                   end:function(){
                       console.log(123);
                       demo.reload();
                   },
                   yes:function(index,dom){
                       var qids=[];
                       var body = layer.getChildFrame('body', index);
                       var iframeWin = window[dom.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                       console.log(body);
                       $(body).find('li.list-group-item').each(function(i,elem){
                           qids.push($(elem).data('id'));
                       });
                       data=[];
                       data.push({name:'qids',value:qids});
                       data.push({name:'id',value:$(body).find('#id').val()});
                       console.log($(body).find('#id').val());
                       data=$.param(data);
                       console.log($(dom).find('li.list-group-item'));
                       $.post("{:url('questionnaire/sort')}",data,function(response){
                           var msg='排序成功';
                           if(response.code>1){
                               msg='排序失败'
                           }
                           layer.msg(msg);
                           setTimeout(function(){layer.close(index)}, 1000);
                       })
                   }
               });
           }
        });

         var active = {
            reload: function(){
                //执行重载
                demo.reload();
            },
            add:function(){
                layer.open({
                    type: 2,
                    title: '添加问卷',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['800px', '600px'],
                    content: '{:url("questionnaire/category_add")}',//iframe的url
                    end:function(){
                        console.log(123);
                        demo.reload();
                    }
                });
            },
            search:function(){
                demo.reload({
                    where: {
                        'name':$('#demoReload').val()
                        }
                });
            },
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //监听是否开放
        form.on('checkbox(lockDemo)', function(obj){
            var lock=!obj.elem.checked;
            $.post("{:url('questionnaire/edit')}",{id:obj.elem.value,lock:lock},function(response){
                var msg='切换成功';
                if(response.code>1){
                    msg='切换失败';
                }
                layer.msg(msg);
            });
        });


        //上传文本
        $('#uploads').change(function(){
            console.log(123);
            var _self=$(this);
            var fd=new FormData();
            fd.append('file',this.files[0]);
            $.ajax({
                url:"{:url('questionnaire/uploadstxt')}",
                type:'POST',
                processData: false,
                contentType: false,
                data:fd,
                success:function(response){
                    layer.msg(response.msg);
                    demo.reload();
                    _self.val('');
                    console.log(_self.val());
                }
            })
        })
    });
</script>


